/*
 * @Date: 2022-09-29 17:49:39
 * @LastEditors: Jason Chen
 * @LastEditTime: 2022-12-05 09:24:21
 * @FilePath: /changeDbPassword/server/main.go
 */
package main

import (
	"log"
	"net/http"
	"os"
	"runtime"

	"github.com/flipped-aurora/gin-vue-admin/server/core"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/initialize"
	"github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
	"github.com/flipped-aurora/gin-vue-admin/server/service"

	_ "net/http/pprof" // 会自动注册 handler 到 http server，方便通过 http 接口获取程序运行采样报告
)

//go:generate go env -w GO111MODULE=on
//go:generate go env -w GOPROXY=https://goproxy.cn,direct
//go:generate go mod tidy
//go:generate go mod download

// @title Swagger Example API
// @version 0.0.1
// @description This is a sample Server pets
// @securityDefinitions.apikey ApiKeyAuth
// @in header
// @name x-token
// @BasePath /
func main() {
	global.GVA_VP = core.Viper() // 初始化Viper
	global.GVA_LOG = core.Zap()  // 初始化zap日志库

	// 初始化数据
	initDb := request.InitDB{
		DBType:   "mysql",
		Host:     global.GVA_CONFIG.Mysql.Path,
		UserName: global.GVA_CONFIG.Mysql.Username,
		Password: global.GVA_CONFIG.Mysql.Password,
		DBName:   global.GVA_CONFIG.Mysql.Dbname,
		Port:     global.GVA_CONFIG.Mysql.Port,
	}
	service.ServiceGroupApp.SystemServiceGroup.InitDBService.InitDB(initDb)

	// gorm连接数据库
	global.GVA_DB = initialize.Gorm()
	initialize.Timer()
	initialize.DBList()

	if global.GVA_DB != nil {
		// 注册表
		initialize.RegisterTables(global.GVA_DB)

		// 增量数据应该放到这里
		service.ServiceGroupApp.SystemServiceGroup.InitDBService.IncrementalMysqlData(initDb)

		// 程序结束前关闭数据库链接
		db, _ := global.GVA_DB.DB()
		defer db.Close()
	}

	//
	profd()

	//
	core.RunWindowsServer()

	// 启动任务
	/* 	taskController := controller.TaskController{}
	   	taskController.Start() */
}

func profd() {
	runtime.SetMutexProfileFraction(1) // 开启对锁调用的跟踪
	runtime.SetBlockProfileRate(1)     // 开启对阻塞操作的跟踪

	go func() {
		// 启动一个 http server，注意 pprof 相关的 handler 已经自动注册过了
		if err := http.ListenAndServe(":6060", nil); err != nil {
			log.Fatal(err)
		}
		os.Exit(0)
	}()
}
